<template>
  <a-form :form="formUpdPwd" style="max-width: 320px;margin-top: 12px">
    <div class="title">修改密码</div>
    <a-form-item
      label="原密码"
      :labelCol="labelCol"
      :wrapperCol="wrapperCol"
      has-feedback
    >
      <a-input-password placeholder="请输入原密码" type="password"
                        v-decorator="['old_password', {rules: [{required: true, message: '请输入原密码！'}]}]"/>
    </a-form-item>
    <a-form-item
      label="新密码"
      :labelCol="labelCol"
      :wrapperCol="wrapperCol"
      has-feedback
    >
      <a-input-password
        placeholder="请输入新密码"
        type="password"
        v-decorator="['new_password', {rules: [{required: true, message: '请输入新密码！'},{
            validator: validateToNextPassword,
          },]}]"/>
    </a-form-item>
    <a-form-item
      label="再次确认"
      :labelCol="labelCol"
      :wrapperCol="wrapperCol"
      has-feedback
    >
      <a-input-password
        placeholder="请再次输入新密码"
        type="password"
        v-decorator="['confirm', {rules: [{required: true, message: '请再次输入新密码！'},
                                            {
                                              validator: compareToFirstPassword,
                                            }]}]"/>
    </a-form-item>
    <a-form-item>
      <a-button type="primary" @click="handleOkUpdPwd">更新密码</a-button>
    </a-form-item>
  </a-form>
</template>

<script>
  import md5 from 'js-md5'
  import { sysUserUpdatePwd } from '@/api/system/userManage'

  export default {
    data() {
      return {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 },
        confirmLoading: false,
        visible: false,
        formUpdPwd: this.$form.createForm(this)
      }
    },
    methods: {
      open() {
        this.visible = true
      },
      handleOkUpdPwd() {
        const { formUpdPwd: { validateFields } } = this
        validateFields((errors, values) => {
          if (!errors) {
            this.confirmLoading = true
            console.log(values, 11)
            values.old_password = md5(values.old_password)
            values.new_password = md5(values.new_password)
            values.confirm = md5(values.confirm)
            sysUserUpdatePwd(values).then((res) => {
              if (res.success) {
                this.$message.success('修改成功')
                this.handleCancel()
              } else {
                this.$message.error('修改失败：' + res.message)
              }
            }).finally((err) => {
              this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.visible = false
      },
      compareToFirstPassword(rule, value, callback) {
        const formUpdPwd = this.formUpdPwd
        if (value && value !== formUpdPwd.getFieldValue('new_password')) {
          callback('请确认两次输入密码的一致性！')
        } else {
          callback()
        }
      },
      validateToNextPassword(rule, value, callback) {
        const formUpdPwd = this.formUpdPwd
        if (value && this.confirmDirty) {
          formUpdPwd.validateFields(['confirm'], { force: true })
        }
        callback()
      }
    }
  }
</script>
